package com.base.common.converter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Convert;
import org.operamasks.faces.annotation.DefineConverter;
import org.operamasks.faces.annotation.Format;

import com.base.commonKey.entity.CommonKeyType;
import com.base.commonKey.service.ICommonKeyTypeDao;
import com.base.commons.SpringBeanHolder;

/**
 * 用于将keyId转换成keyValue的转换类
 * 
 * 
 */

@DefineConverter(id = "commonKeyTypeFormatter")
public class CommonKeyTypeConverter implements Converter {
	private final static Log logger = LogFactory
			.getLog(CommonKeyTypeConverter.class);

	ICommonKeyTypeDao ckDao = SpringBeanHolder.getBean("commonKeyTypeDao");

	@Format
	public String format(Object value) {
		if (value == null || "".equals(value.toString()))
			return "";
		String typecode = value.toString();

		String result = "";
		List<CommonKeyType> cks = ckDao.findAllByNamedQuery(
				"commonKeyType.getTypeByTypeCode", typecode);

		if (cks != null && cks.size() > 0) {
			for (CommonKeyType rw : cks) {
				result += rw.getTypeName();
			}
		}

		return result;
	}

	@Convert
	public Object convert(String value) {
		return null;
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (value == null || "".equals(value.toString()))
			return "";
		String typecode = value.toString();

		String result = "";
		List<CommonKeyType> cks = ckDao.findAllByNamedQuery(
				"commonKeyType.getTypeByTypeCode", typecode);

		if (cks != null && cks.size() > 0) {
			for (CommonKeyType rw : cks) {
				result += rw.getTypeName();
			}
		}

		return result;
	}

}
